const baseUrl = uni.$baseUrl //'http://47.100.32.115:8080/jeecgboot/api/colorpath'
let str = '/nailAppUser/wxLogin'
// token 过期
function noToken(){
	let curPage = getCurrentPages();
	let route = curPage[curPage.length - 1].route; //获取当前页面的路由
	let params = curPage[curPage.length - 1].options; //获取当前页面参数
	let query = '';
	let keys = Object.keys(params); //获取对象的key 返回对象key的数组
	if (keys.length > 0) {
		query = keys.reduce((pre, cur) => {
			return pre + cur + '=' + params[cur] + '&';
		}, '?').slice(0, -1);
	}
	uni.showModal({
		title: '登录过期',
		content: '登录信息已过期，请重新登录',
		// cancelText: "取消", // 取消按钮的文字
		confirmText: "去登录", // 确认按钮的文字
		showCancel: false, // 是否显示取消按钮，默认为 true
		// confirmColor: '#E4919D',
		cancelColor: '#39B54A',
		success: (res) => {
			if(res.confirm) {
				console.log('comfirm') //点击确定之后执行的代码
				uni.clearStorage();
				uni.redirectTo({
					url: '/pagesC/login/login?obj=' + (route + query),
					animationType: 'pop-in',
					animationDuration: 300
				})
			} else {
				console.log('cancel') //点击取消之后执行的代码
			}
		}
	})
}
export default class Request {
    http(param) {
        // 请求参数
        let url = param.url,
            method = param.method,
            header = {},
            data = param.data || param.params,
            token = uni.getStorageSync('token') || "",
            hideLoading = param.hideLoading || false;
			
        let requestUrl = baseUrl + url;
        //请求方式:GET或POST(POST需配置
        // header: {'content-type' : "application/x-www-form-urlencoded"},)
        if (method) {
            method = method.toUpperCase(); //小写改为大写
            if (method == "POST") {
				if(url.indexOf(str) < 0) {
					header = {
					    'content-type': "application/json",
						'X-Access-Token': token
					};
				}
				else if(url.indexOf('nailShow/createUserShow') > 0) {
					header = {
					    'content-type': "application/x-www-form-urlencoded",
						'X-Access-Token': token
					}
				}
				else{
					header = {
					    'content-type': "application/x-www-form-urlencoded"
					}
				}
                
            } else {
				if(url.indexOf(str) < 0) {
					header = {
					    'content-type': "application/x-www-form-urlencoded",
						'X-Access-Token': token
					};
				}else{
					header = {
					    'content-type': "application/x-www-form-urlencoded"
					}
				}
            }
        }

        // 加载圈 
        if (!hideLoading && requestUrl.indexOf('nailTerminal/querylis') < 0) {
            uni.showLoading({
                title: '加载中...'
            });
        }

        // 返回promise
        return new Promise((resolve, reject) => {
            // 请求
            uni.request({
                url: requestUrl,
                data: data,
                method: method,
                header: header,
                success: (res) => {
					console.log('正常')
                    // 判断 请求api 格式是否正确
                    if (res.statusCode && res.statusCode != 200) {
                        uni.showToast({
                            title: res.errMsg || '网络异常',
                            icon: 'none'
                        });
                        return;
                    }
					// 登录过期
					if(res.data.code == 1000 || (url.indexOf(str) < 0 && token == "")) {
						noToken()
						return
					}
                    if(res.data.code == 200){
						// 将结果抛出
						resolve(res.data.result)
					}else{
						if(url.indexOf('/nailGoodsInfo/cartList') <0){
							uni.showToast({
							    title: res.data.message || '网络异常',
							    icon: 'none'
							});
						}
						reject(res.data)
					}
                },
                //请求失败
                fail: (e) => {
					console.log('错误', e)
                    uni.showToast({
                        title: e.data && e.data.message || "网络异常~",
                        icon: 'none'
                    });
                    reject(e.data);
                },
                //请求完成
                complete() {
                    //隐藏加载
                    if (!hideLoading && requestUrl.indexOf('nailTerminal/querylis') < 0) {
                        uni.hideLoading();
                    }
                    // resolve();
                    return;
                }
            })
        })
    }
}